package com.ihrm.company.controller;


import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.common.exception.CommonException;
import com.ihrm.company.service.CompanyService;
import com.ihrm.domain.company.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//解决跨域问题
@CrossOrigin
@RestController
@RequestMapping(value = "/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    // 保存企业信息
    @RequestMapping(value = "",method = RequestMethod.POST)
    public Result save(@RequestBody Company company){
        //业务操作
        companyService.add(company);
        return new Result(ResultCode.SUCCESS);
    }
    /*
    1. 方法
    2. 请求参数
    3. 响应
     */
    // 根据id更新企业信息
    @RequestMapping(value = "/{id}",method = RequestMethod.PUT)
    public Result update(@PathVariable(value = "id") String id,@RequestBody Company company){
        // 业务操作
        company.setId(id);
        companyService.update(company);
        return new Result(ResultCode.SUCCESS);
    }
    // 根据id删除企业信息  测试自定义异常类
//    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
//    public Result delete(@PathVariable(value = "id")String id) throws CommonException {
//        throw new CommonException(ResultCode.UNAUTHORISE);
//        //业务操作
////        companyService.deleteById(id);
////        return new Result(ResultCode.SUCCESS);
//    }

    // 根据id删除企业信息
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id")String id){
        //业务操作
        companyService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }

    // 根据id查询企业信息
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Result findById(@PathVariable(value = "id")String id){
        Company company = companyService.findById(id);
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(company);
        return result;
    }

    // 查看企业列表
    @RequestMapping(value = "",method = RequestMethod.GET)
    public Result findAll(){
//        int i = 1/0;   测试自定义公共异常类
        List<Company> list = companyService.findAll();
        Result result = new Result(ResultCode.SUCCESS);
        result.setData(list);
        return  result;
    }
}
